package cn.com.aiouyi.controller.admin;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.com.aiouyi.common.Message;
import cn.com.aiouyi.entity.Navigation;
import cn.com.aiouyi.page.Pageable;
import cn.com.aiouyi.service.ArticleClassService;
import cn.com.aiouyi.service.ArticleService;
import cn.com.aiouyi.service.NavigationPositionService;
import cn.com.aiouyi.service.NavigationService;
import cn.com.aiouyi.vo.Link;

/**
 * Controller - 导航
 * @author hanson
 *
 */
@Controller("adminNavigationController")
@RequestMapping("/admin/navigation")
public class NavigationController extends BaseController{

	@Resource(name = "navigationServiceImpl")
	private NavigationService navigationService;
	@Resource(name = "navigationPositionServiceImpl")
	private NavigationPositionService navigationPositionService;
	@Resource(name = "articleClassServiceImpl")
	private ArticleClassService articleClassService;
	@Resource(name = "articleServiceImpl")
	private ArticleService articleService;
	
	@GetMapping("/list")
	public String list(Pageable pageable, ModelMap model){
		
		model.put("page", navigationService.findPage(pageable));
		return "/admin/navigation/list";
	}
	
	@GetMapping("/articleList")
	public String articleList(Pageable pageable, ModelMap model){
		
		model.put("page", articleService.findPage(pageable));
		return "/admin/navigation/articleList";
	}
	
	@GetMapping("/add")
	public String add(ModelMap model){
		model.put("navigationPositions", navigationPositionService.findAll());
		model.put("navigationTree", navigationService.findTree());
		model.put("articleClasses", articleClassService.findAll());
		model.put("linkTypes", Link.Type.values());
		return "/admin/navigation/add";
	}
	
	@PostMapping("/save")
	public String save(@Valid Navigation navigation, Long navigationPositionId, Long parentId, RedirectAttributes redirectAttributes){
		navigation.setNavigationPosition(navigationPositionService.find(navigationPositionId));
		navigation.setParent(navigationService.find(parentId));
		
		navigationService.save(navigation);
		
		addFlashMessage(redirectAttributes, Message.SUCCESS);
		return "redirect:list";
	}
	
	@GetMapping("/edit")
	public String edit(Long id, ModelMap model){
		Navigation navigation = navigationService.find(id);
		
		model.put("navigation", navigation);
		model.put("navigationPositions", navigationPositionService.findAll());
		model.put("navigationTree", navigationService.findTree());
		model.put("children", navigationService.findChildren(navigation, true, null));
		model.put("articleClasses", articleClassService.findAll());
		model.put("linkTypes", Link.Type.values());
		return "/admin/navigation/edit";
	}
	
	@PostMapping("/update")
	public String update(Long id, @Valid Navigation navigation, 
			Long navigationPositionId, Long parentId, ModelMap model, RedirectAttributes redirectAttributes){
		navigation.setNavigationPosition(navigationPositionService.find(navigationPositionId));
		navigation.setParent(navigationService.find(parentId));
		
		if (navigation.getParent() != null) {
			Navigation parent = navigation.getParent();
			if (parent.equals(navigation)) {
				model.put(ERROR_VIEW_MESSAGE_KEY, "子级导航不能跟父级导航一样");
				return ERROR_VIEW;
			}
			List<Navigation> children = navigationService.findChildren(parent, true, null);
			if (children != null && children.contains(parent)) {
				model.put(ERROR_VIEW_MESSAGE_KEY, "导航不允许选择自己原来的子级作为父级");
				return ERROR_VIEW;
			}
		}
		
		navigationService.update(navigation);
		
		addFlashMessage(redirectAttributes, Message.SUCCESS);
		return "redirect:list";
	}
	
	@PostMapping("/delete")
	@ResponseBody
	public Message delete(Long[] ids){
		
		navigationService.realDelete(ids);
		
		return Message.SUCCESS;
	}
}
